package com.mcm.lstudy.dao.impl;

import java.util.List;

import com.mcm.lstudy.dao.UserDao;
import com.mcm.lstudy.entity.User;
import com.mcm.lstudy.tools.DBTools;
import com.mcm.lstudy.tools.MD5Util;
import com.mcm.lstudy.tools.PageData;

public class UserDaoImpl implements UserDao {
	@Override
	public int doLogin(String username, String password) {
		String upassword = MD5Util.getEncodeByMd5(password);
		String sql = "select * from user where (username=? or utel=?) and upassword=? and ustatus=1";
		List<User> li = (List<User>) DBTools.exQuery(sql, User.class, username, username, upassword);
		if (li.size() == 0) {
			return -1;
		}
		return li.get(0).getUidentity();
	}

	@Override
	public boolean doRegister(User user) {
		if (user == null) {
			return false;
		}
		String sql = "insert into user values(null,?,?,?,?,?,?,?,?,?,?)";
		return DBTools.exUpdate(sql, user.getUname(), user.getUsername(), user.getUpassword(), user.getUtel(),
				user.getUemail(), user.getUheadimg(), user.getUsex(), user.getUstatus(), user.getUidentity(),
				user.getUdatetime()) > 0;
	}

	@Override
	public User getUserByName(String username) {
		String sql = "select * from user where username=? ";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<User> getUsers(String keyword) {
		String sql = "select * from user where username like ?";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, "%" + keyword + "%");
		return list;
	}

	@Override
	public List<User> getTeachers(String keyword) {
		String sql = "select * from user where username like ? and uidentity = 2";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, "%" + keyword + "%");
		return list;
	}

	@Override
	public User getTeachersByCid(int uid) {
		String sql = "select * from user where uid = ? and uidentity = 2";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, uid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public PageData<User> getUserByPage(int page, int pageSize, String keyword) {
		// TODO Auto-generated method stub
		return DBTools.exQueryByPage("select * from user where username like ?", User.class, page, pageSize,
				"%" + keyword + "%");
	}

	@Override
	public boolean delUser(int id) {
		String sql = "update user set ustatus = 0 where uid = ?";
		return DBTools.exUpdate(sql, id) > 0;
	}

	@Override
	public boolean updateUser(User user, int uid) {
		String sql = "update user set uname =?,username=?,utel=?,uemail=?,uheadimg=?,usex=?,uidentity=? where uid =?";
		return DBTools.exUpdate(sql, user.getUname(), user.getUsername(), user.getUtel(), user.getUemail(),
				user.getUheadimg(), user.getUsex(), user.getUidentity(), uid) > 0;
	}

	@Override
	public boolean updateUserPassword(String password, int uid) {
		String upassword = MD5Util.getEncodeByMd5(password);
		String sql = "update user set upassword = ? where uid = ?";
		return DBTools.exUpdate(sql, upassword, uid) > 0;
	}

	@Override
	public User getUserById(int uid) {
		String sql = "select * from user where uid=? ";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, uid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);

	public PageData<User> getUserByPage(int page, int pageSize, String keyword) {
		// TODO Auto-generated method stub
		return DBTools.exQueryByPage("select * from user where username like ?", User.class, page, pageSize,
				"%" + keyword + "%");
	}

	@Override
	public boolean delUser(int id) {
		String sql = "update user set ustatus = 0 where uid = ?";
		return DBTools.exUpdate(sql, id) > 0;
	}

	@Override
	public boolean updateUser(User user, int uid) {
		String sql = "update user set uname =?,username=?,utel=?,uemail=?,uheadimg=?,usex=?,uidentity=? where uid =?";
		return DBTools.exUpdate(sql, user.getUname(), user.getUsername(), user.getUtel(), user.getUemail(),
				user.getUheadimg(), user.getUsex(), user.getUidentity(), uid) > 0;
	}

	@Override
	public boolean updateUserPassword(String password, int uid) {
		String upassword = MD5Util.getEncodeByMd5(password);
		String sql = "update user set upassword = ? where uid = ?";
		return DBTools.exUpdate(sql, upassword, uid) > 0;
	}

	@Override
	public User getUserById(int uid) {
		String sql = "select * from user where uid=? ";
		List<User> list = (List<User>) DBTools.exQuery(sql, User.class, uid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);

	}

}
